﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using Core.DAL;

namespace xManagerWebApp.__Membership
{
    public partial class WF_ViewProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String req = Request.QueryString["user"];                   

            using (DataAccessLayer dc = new DataAccessLayer())
            {
                Guid search = req == null ? Core.User.CurrentUserId.Value : new Guid(req);

                var query = from pfile in dc.DataContext.Profiles
                            where pfile.Id.Equals(search)
                            select pfile;
                   
                Profile p = query.SingleOrDefault();
                if (p == null)
                {
                    p = new Profile();
                    p.Id = Core.User.CurrentUserId.Value;
                    p.Adress = p.Cellphone = p.Fax = p.FullName = p.NIB = p.Homephone = "";
                    p.Photo = null;
                    dc.DataContext.Profiles.InsertOnSubmit(p);
                    dc.DataContext.SubmitChanges();

                    User u = dc.DataContext.Users.SingleOrDefault(elem => elem.Id.Equals(Core.User.CurrentUserId.Value));
                    legend.Text = u.Shortname + " profile";
                }
                else
                {
                    legend.Text = p.User.Shortname + " profile";
                    this.FullName.Text = p.FullName;
                    this.Morada.Text = p.Adress;
                    this.NIB.Text = p.NIB;
                    this.Telefone.Text = p.Homephone;
                    this.Telemovel.Text = p.Cellphone;
                }
                if (search.Equals(Core.User.CurrentUserId.Value))
                    this.edit_profile.Visible = true;
            }
        }
    }
}

